% function [hGA,hGG]=generate_channel1127(paraSystem,loc_BS_ATG,loc_airplane,loc_BS,loc_user)
function [hGA,hGG]=generate_channel1127(paraSystem,loc_airplane,loc_BS,loc_user)
% [hGA,hGG,hAA,hAG]
MA=paraSystem.MA; 
MG=paraSystem.MG; 
N=paraSystem.N; 
K=paraSystem.K;
freq_=paraSystem.freq_; % 4GHz C band

% loc_BS_ATG = loc_BS_ATG * 1000;
loc_airplane = loc_airplane * 1000;
loc_BS = loc_BS * 1000;
loc_user = loc_user * 1000;

% 地面发射，地面接收：rayleigh，全是NLOS
% 地面基站发射，空中飞机接收：Rician，有LOS和NLOS
Ricianfactor=10;

distGG = cell(N,N,K);
distGA = ones(N,1);
distAG = cell(N,K);
atteGG = cell(N,N,K);
atteGA = ones(N,1);
atteAG = cell(N,K);
hGG=cell(N,N,K);
hGA=cell(N,1);
hAG=cell(N,K);

for nn=1:N
    for kk=1:K
        % TBSm→TUnk
        % Rayleigh fading
        for mm=1:N
            distGG{mm,nn,kk} = sqrt(sum(abs(loc_BS(mm,:)-loc_user((nn-1)*K+kk)).^2));
            atteGG{mm,nn,kk} = sqrt(pathloss_NLOS(distGG{mm,nn,kk},freq_));
            HNLOS=sqrt(1/2).*(randn(MG,1)+1j.*randn(MG,1));
            hGG{mm,nn,kk}=atteGG{mm,nn,kk}*HNLOS;
        end
%         % ABS→TUnk
%         % Rayleigh fading
%         distAG{nn,kk} = sqrt(sum(abs(loc_BS_ATG-loc_user((nn-1)*K+kk)).^2));
%         atteAG{nn,kk} = sqrt(pathloss_NLOS(distAG{nn,kk},freq_));
%         HNLOS=sqrt(1/2).*(randn(MA,1)+1j.*randn(MA,1));
%         hAG{nn,kk}=atteAG{nn,kk}*HNLOS;
    end
    % TBSn→airplane     
    % N MG-antenna transmitters → 1 single-antenna receiver
    % Rician fading
    distGA(nn) = sqrt(sum(abs(loc_BS(nn,:)-loc_airplane).^2));
    atteGA(nn) = sqrt(pathloss_LOS(distGA(nn),freq_));
    User_angle=rand;
    HNLOS=sqrt(1/2).*(randn(MG,1)+1j.*randn(MG,1));
    hGA{nn}=atteGA(nn).*channel_MISO_rician(MG,Ricianfactor,User_angle,HNLOS);
end
%% ABS→
% % ABS→airplane
% % 1 MA-antenna transmitter → 1 single-antenna receiver
% % Rician fading
% distAA = sqrt(sum(abs(loc_BS_ATG-loc_airplane).^2));
% atteAA = sqrt(pathloss_LOS(distAA,freq_));
% User_angle=rand;
% HNLOS=sqrt(1/2).*(randn(MA,1)+1j.*randn(MA,1));
% hAA=atteAA.*channel_MISO_rician(MA,Ricianfactor,User_angle,HNLOS);


end

% function gggggdB = ES_antenna_gain(off_boresight_angle)
% diameter_antenna = 1;
% lambda = 1;
% max_antenna_gain_earth_station = 1;
% fai_m = 20*lambda/diameter_antenna * sqrt(max_antenna_gain_earth_station - (2+15*log(diameter_antenna/lambda)));
% fai_r = 15.85*(diameter_antenna/lambda)^(-0.6);
% if 0<off_boresight_angle && off_boresight_angle<fai_m
%     gggggdB = max_antenna_gain_earth_station - 2.5*10^(-3)*(diameter_antenna/lambda*off_boresight_angle)^2;
% elseif fai_m<off_boresight_angle && off_boresight_angle<fai_r
%     gggggdB = 2+15*log(diameter_antenna/lambda);
% elseif fai_r<off_boresight_angle && off_boresight_angle<48
%     gggggdB = 32-25*log(off_boresight_angle);
% elseif 48<off_boresight_angle && off_boresight_angle<180
%     gggggdB = -10;
% end
% end


function Hr=channel_MISO_rician(num_antenna,Ricianfactor,User_angle,Hr_sig)
eb2=1/(1+Ricianfactor);
eb1=1-eb2;
eb1=sqrt(eb1);
eb2=sqrt(eb2);
steervec=exp(1j*pi*sin(User_angle).*(0:num_antenna-1)');
% 1 M-antenna transmitter → 1 single-antenna receivers
% Hr=eb1.*(ULA_fun(M,User_angle))+eb2.*Hr_sig;
Hr=eb1.*steervec + eb2.*Hr_sig;
end

function loss = pathloss_free_space(dist_,freq_)
% free space loss
c=3*10^8;
loss=(c/(4*pi*dist_*freq_))^2;
% lossdB=32.44+20*log10(dist_)+20*log10(freq_/10^9);
% loss=10^(-lossdB/10);
end

function loss = pathloss_LOS(dist_,freq_)
% the path-loss is set according to the 3GPP propagation environment [52, Table B.1.2.1-1]
% https://www.3gpp.org/ftp/specs/archive/36_series/36.814/
lossdB=28 + 22*log10(dist_) + 20*log10(freq_/10^9);
loss=10^(-lossdB/10);
end

function loss = pathloss_NLOS(dist_,freq_)
% dist_ = 1200;
% freq_ = 18*10^9;
% the path-loss is set according to the 3GPP propagation environment [52, Table B.1.2.1-1]
% https://www.3gpp.org/ftp/specs/archive/36_series/36.814/
lossdB=22.7 + 36.7*log10(dist_) + 26*log10(freq_/10^9);
loss=10^(-lossdB/10);
% loss * 20000
end


% dist_=1000*1000; % 1000km LEO
% freq_=4*10^9; % 4GHz C band
% c=3*10^8;
% LL=(4*pi*dist_*freq_/c)^2;
% 10*log10(LL)
% LL=92.44+20*log10(dist_/1000)+20*log10(freq_/10^9)
% LL=92.44+20*(log10(dist_)-3)+20*log10(freq_GHz)
% LL=92.44+20*log10(dist_)-60+20*log10(freq_GHz)
% LL=32.44+20*log10(dist_)+20*log10(freq_GHz)
% LL=32.44+20*log10(dist_)+20*log10(freq_GHz)
% LL=35.6+22*log10(dist_)

% noise=-170+10*log10(180*1000) % =-117.4473dBm
% noise=10^(-170/10)*180*1000;%mW
% 10*log10(noise)%=-117.4473dBm

%% 











